package com.cskaoyan.service.systemservice;

import com.cskaoyan.bean.systembean.*;
import com.cskaoyan.bo.systembo.PermissionBo;
import com.cskaoyan.mapper.systemmapper.PermissionMapper;
import com.cskaoyan.mapper.systemmapper.RoleMapper;
import com.cskaoyan.vo.systemvo.RoleListVo;
import com.cskaoyan.vo.systemvo.RoleOptionsVo;
import com.cskaoyan.vo.systemvo.RolePermissionPostVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;

    /**
     * @return 返回所有角色，返回vo为value=id，label=name
     */
    @Override
    public List<RoleOptionsVo> options() {
        //得到数据集
        List<Role> roles = roleMapper.selectByExample(new RoleExample());
        //塞入vo
        Iterator<Role> iterator = roles.iterator();
        ArrayList<RoleOptionsVo> roleOptionsVos = new ArrayList<>();
        while (iterator.hasNext()) {
            Role role = iterator.next();
            RoleOptionsVo roleOptionsVo = new RoleOptionsVo();
            roleOptionsVo.setValue(role.getId());
            roleOptionsVo.setLabel(role.getName());
            roleOptionsVos.add(roleOptionsVo);
        }

        return roleOptionsVos;
    }

    /**
     * 按条件查询角色
     *
     * @param page
     * @param limit
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @Override
    public RoleListVo list(Integer page, Integer limit, String name, String sort, String order) {
        //设置排序规则
        RoleExample roleExample = new RoleExample();
        if (name != null && !"".equals(name)) {
            RoleExample.Criteria criteria = roleExample.createCriteria();
            criteria.andNameLike("%" + name + "%");
        }
        roleExample.setOrderByClause(sort + " " + order);

        //得到数据集
        List<Role> roles = roleMapper.selectByExample(roleExample);

        //to vo
        RoleListVo roleListVo = new RoleListVo();
        roleListVo.setTotal(roles.size());
        //设置分页
        PageHelper.startPage(page, limit);
        roleListVo.setItems(roles);


        return roleListVo;
    }

    /**
     * 新增并返回
     *
     * @param role
     * @return
     */
    @Override
    public Role create(Role role) {
        roleMapper.insertSelective(role);
        return roleMapper.selectByPrimaryKey(role.getId());
    }

    /**
     * 改name和desc
     *
     * @param role
     */
    @Override
    public void update(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 删
     *
     * @param role
     */
    @Override
    public void delete(Role role) {
        //物理删除
        roleMapper.deleteByPrimaryKey(role.getId());
//        //逻辑删除
//        Role role1 = new Role();
//        role1.setId(role.getId());
//        role1.setDeleted(true);
//        roleMapper.updateByPrimaryKeySelective(role1);
    }

    @Override
    public RolePermissionPostVo permissions1(Integer roleId) {
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<Permission> permissions = permissionMapper.selectByExample(permissionExample);
        //只要permission部分
        Iterator<Permission> iterator = permissions.iterator();
        ArrayList<String> strings = new ArrayList<>();
        while (iterator.hasNext()) {
            Permission next = iterator.next();
            strings.add(next.getPermission());
        }
        SystemPermissons systemPermissons = new SystemPermissons();
        return new RolePermissionPostVo(strings, systemPermissons.getSystemPermissonsFirstLevelList());

    }

    @Override
    public void permissions2(PermissionBo permissionBo) {
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andRoleIdEqualTo(permissionBo.getRoleId());
        //先删除
        permissionMapper.deleteByExample(permissionExample);
        //再改动
        int roleId = permissionBo.getRoleId();
        List<String> permissions = permissionBo.getPermissions();
        Iterator<String> iterator = permissions.iterator();
        while (iterator.hasNext()) {
            String permission = iterator.next();
            Permission permission1 = new Permission();
            permission1.setRoleId(roleId);
            permission1.setPermission(permission);
            permissionMapper.insert(permission1);
        }
    }
}
